import requests
from bs4 import BeautifulSoup
import os
from urllib.parse import urljoin

def scrape_resume_website(url):
    try:
        # 创建保存图片的目录
        if not os.path.exists('resume_images'):
            os.makedirs('resume_images')
        
        # 发送HTTP请求
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        response = requests.get(url, headers=headers)
        response.encoding = 'utf-8'  # 明确设置编码
        response.raise_for_status()
        
        soup = BeautifulSoup(response.text, 'html.parser')
        
        # 1. 提取基本信息
        profile = soup.find('div', class_='profile')
        name = profile.find('h1').get_text(strip=True) if profile else "未找到姓名"
        title = profile.find('p').get_text(strip=True) if profile and profile.find('p') else "未找到求职意向"
        
        # 2. 提取联系方式
        contact_info = soup.find('div', class_='contact-info')
        contact = contact_info.get_text(strip=True, separator='\n') if contact_info else "未找到联系方式"
        
        # 3. 提取各章节内容
        def get_section_content(title):
            section = soup.find('h2', string=title)
            if section:
                content = section.find_next('div', class_='content')
                return content.get_text(strip=True, separator='\n') if content else f"未找到{title}内容"
            return f"未找到{title}标题"
        
        # 4. 提取并保存图片
        img = soup.find('img', class_='profile-img')
        if img and img.get('src'):
            img_url = urljoin(url, img['src'])
            try:
                img_data = requests.get(img_url, headers=headers).content
                img_name = f"resume_images/{name}.jpg"
                with open(img_name, 'wb') as f:
                    f.write(img_data)
                img_status = f"个人图片已保存为: {img_name}"
            except Exception as e:
                img_status = f"图片下载失败: {e}"
        else:
            img_status = "未找到个人图片"
        
        # 打印所有信息
        print("="*50)
        print(f"姓名: {name}")
        print(f"求职意向: {title}")
        print("\n联系方式:")
        print(contact)
        print("\n教育背景:")
        print(get_section_content("教育背景"))
        print("\n技能专长:")
        print(get_section_content("技能专长"))
        print("\n项目经验:")
        print(get_section_content("项目经验"))
        print("\n获奖情况:")
        print(get_section_content("获奖情况"))
        print("\n自我评价:")
        print(get_section_content("自我评价"))
        print("\n" + img_status)
        print("="*50)
        
    except Exception as e:
        print(f"爬取过程中出现错误: {e}")

if __name__ == "__main__":
    target_url = "http://39.108.97.198/"
    print(f"开始爬取简历网站: {target_url}")
    scrape_resume_website(target_url)
    print("爬取完成!")